/*
ONEGAME
Copyright (C) 2009  Pablo Caballero

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package one.main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import one.map.ILayerMap;
import one.project.ILayerManager;
import one.project.IProject;

/**
 * Map Panel
 * @author Pablo Caballero
 * @version 1.0 16-jun-2009
 */
public class MapPanel extends javax.swing.JPanel {

    /**
     * Project
     */
    private IProject project;
    protected Image backBuffer;

    /** Creates new form MapPanel */
    public MapPanel() {
        initComponents();
        resizeBuffer();
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2, gbackbuffer;
        Color color_prev;
        ILayerManager sysmap;
        ILayerMap layer;
        Image buffer;
        long utc;

        g2 = (Graphics2D) g;

        super.paint(g);
        color_prev = g2.getColor();
        g2.setColor(new Color(0, 74, 127));
        g2.fillRect(0, 0, this.getWidth(), getHeight());
        g2.setColor(color_prev);

        if (getProject() != null) {
            gbackbuffer = (Graphics2D) backBuffer.getGraphics();
            sysmap = getProject().getSystemMaps();
            utc = System.currentTimeMillis();
            for (int i = 0; i < sysmap.getLayerCount(); i++) {
                layer = sysmap.getLayer(i);
                if (layer.isVisible()) {
                    buffer = layer.paint(utc);
                    gbackbuffer.drawImage(buffer, 0, 0, this);
                }
            }
            g2.drawImage(backBuffer, 0, 0, this);
        }
    }

    /**
     * Get the value of project
     * @return the value of project
     */
    public IProject getProject() {
        return project;
    }

    /**
     * Sets the project in the model of the tool panel
     * @param project the project
     */
    void setProject(IProject project) {
        this.project = project;
    }

    /**
     * Get the value of backBuffer
     * @return the value of backBuffer
     */
    public Image getBackBuffer() {
        return backBuffer;
    }

    /**
     * Set the value of backBuffer
     * @param backBuffer new value of backBuffer
     */
    public void setBackBuffer(Image backBuffer) {
        this.backBuffer = backBuffer;
    }

    private void resizeBuffer() {
        int width, height;
        width = getWidth() <= 0 ? 1 : getWidth();
        height = getHeight() <= 0 ? 1 : getHeight();
        backBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);

        if (getProject() != null) {
            getProject().getSystemMaps().resize(width, height);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        resizeBuffer();
    }//GEN-LAST:event_formComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
